package com.test.ad.demo;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 底部Tab容器：切换“首页”(扫雷) 与 “娱乐”(看广告)。
 */
public class MainTabActivity extends Activity implements View.OnClickListener {

    private LinearLayout tabHome;
    private LinearLayout tabFun;
    private TextView tvHome;
    private TextView tvFun;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab);

        tabHome = findViewById(R.id.tab_home);
        tabFun = findViewById(R.id.tab_fun);
        tvHome = findViewById(R.id.tv_home);
        tvFun = findViewById(R.id.tv_fun);

        tabHome.setOnClickListener(this);
        tabFun.setOnClickListener(this);

        switchTo(new GameFragment());
        updateTab(true);
    }

    private void switchTo(Fragment fragment) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_container, fragment);
        ft.commitAllowingStateLoss();
    }

    private void updateTab(boolean isHome) {
        tabHome.setSelected(isHome);
        tabFun.setSelected(!isHome);
        tvHome.setSelected(isHome);
        tvFun.setSelected(!isHome);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tab_home) {
            updateTab(true);
            switchTo(new GameFragment());
        } else if (v.getId() == R.id.tab_fun) {
            updateTab(false);
            switchTo(new EntertainmentFragment());
        }
    }
}


